package com.huaxin.hxcloud.activity;


import com.huaxin.common.BaseActivity;

/**
 * Created by Administrator on 2016/9/21.
 * 站点 详情界面
 */
public class StationInfoActivity extends BaseActivity {


 /*   @BindView(R.id.share)
    ImageView               share;
    @BindView(R.id.toolbar)
    Toolbar                 toolbar;
    @BindView(R.id.img)
    ImageView               img;
    @BindView(R.id.more)
    ImageView               more;
    @BindView(R.id.tv_org)
    TextView                tvOrg;
    @BindView(R.id.ll_org)
    LinearLayout            llOrg;
    @BindView(R.id.tv_address)
    TextView                tvAddress;
    @BindView(R.id.ll_address)
    LinearLayout            llAddress;
    @BindView(R.id.tv_owner)
    TextView                tvOwner;
    @BindView(R.id.tv_phone)
    TextView                tvPhone;
    @BindView(R.id.ll_owner)
    LinearLayout            llOwner;
    @BindView(R.id.tv_processCapacity)
    TextView                tvProcessCapacity;
    @BindView(R.id.tv_servicePeople)
    TextView                tvServicePeople;
    @BindView(R.id.list_view)
    RecyclerView            rcl_vehicle_record;
    @BindView(R.id.pull_view)
    PullToRefreshScrollView pullView;
    @BindView(R.id.content)
    FrameLayout             content;
    private ArrayList<Device> dataList = new ArrayList<>();
    private VehicleRecordListAdapter  workTaskListAdapter;
    private String     _stationCode;
    private StationDTO _station;
    //华迈视频 回调状态
    private static final int EVENT_CONNECT_SUCCESS = 1;
    private static final int EVENT_CONNECT_FAIL    = 2;
    private static final int EVENT_LOGIN_SUCCESS   = 3;
    private static final int EVENT_LOGIN_FAIL      = 4;
    private Handler mHandler;


    public static void start(Context context, String stationCode) {
        Intent intent = new Intent(context, StationInfoActivity.class);
        intent.putExtra(Constants.IK_STATION_CODE, stationCode);
        context.startActivity(intent);
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        if (extras.containsKey(Constants.IK_STATION_CODE)) {
            _stationCode = extras.getString(Constants.IK_STATION_CODE);
        } else {
            finish();
        }

        registerHander();
    }


    @Override
    protected int getContentViewId() {
        return R.layout.activity_station_info;
    }

    @Override
    protected int getLoadFrameId() {
        return R.id.content;
    }

    @Override
    public void initView() {
        setSupportActionBar(toolbar);
        pullView.setHeaderLayout(new CustomHeaderLayout(this));
        pullView.setOnRefreshListener(this);
        rcl_vehicle_record.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL,
                false));
        share.setImageResource(R.drawable.icon_share);
        workTaskListAdapter = new VehicleRecordListAdapter();
        rcl_vehicle_record.setAdapter(workTaskListAdapter);
    }

    @Override
    protected void requestData() {
        ApiGetStationInfo(_stationCode, null);
    }

    private void ApiGetStationInfo(final String stationCode, final PullToRefreshBase refreshView) {
        if (refreshView == null) {
            updateLoadStatus(LOADING);
        }
        Call<StationDTO> call = HxTransmitRequestHelper.getRequestApi().getStationInfo(stationCode);
        autoClose(call);
        call.enqueue(new Callback<StationDTO>() {
            @Override
            public void onResponse(Call<StationDTO> call, Response<StationDTO> response) {
                StationDTO stationDTO = response.body();
                if (response.isSuccessful() && stationDTO != null) {
                    if (stationDTO.isSuccessful()) {
                        if (refreshView == null) {
                            updateLoadStatus(LOAD_SUCCESS);
                        } else {
                            refreshView.onRefreshComplete();
                        }
                        //                        updateLoadStatus(LOAD_SUCCESS);
                        _station = stationDTO;
                        updateView();
                    } else {
                        if (refreshView == null) {
                            updateLoadStatus(LOAD_ERROR);
                        } else {
                            refreshView.onRefreshComplete();
                        }
                        //                        updateLoadStatus(LOAD_ERROR);
                    }
                } else {
                    if (refreshView == null) {
                        updateLoadStatus(LOAD_ERROR);
                    } else {
                        refreshView.onRefreshComplete();
                    }
                    //                    updateLoadStatus(LOAD_ERROR);
                }
            }

            @Override
            public void onFailure(Call<StationDTO> call, Throwable t) {
                if (refreshView == null) {
                    updateLoadStatus(LOAD_FAILURE);
                } else {
                    refreshView.onRefreshComplete();
                }
                //                updateLoadStatus(LOAD_FAILURE);
            }
        });
    }

    private void updateView() {
        if (_station == null)
            return;
        toolbar.setTitle(_station.getStationName());
        loadNetImg(img, _station.getStationImg());
        loadText(tvOrg, _station.getOrgName(), "暂无");
        loadText(tvOwner, _station.getContactPerson(), "暂无");
        loadText(tvPhone, _station.getContactPhone());
        loadText(tvAddress, _station.getAddress(), "暂无");
        loadText(tvProcessCapacity, _station.getProcessCapacity() + "(吨/天)");
        loadText(tvServicePeople, _station.getServicePeople() + "人");
        if (_station.getDeviceList() != null) {
            dataList.clear();
            dataList.addAll(_station.getDeviceList());
            workTaskListAdapter.notifyDataSetChanged();
        }
    }

    @OnClick({R.id.more, R.id.share, R.id.video_img})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.share:
                final String code = _station.getStationCode();
                final String name = _station.getStationName();
                final String status = "" + _station.getStationState();
                final String address = TextUtils.isEmpty(_station.getAddress()) ? " " : _station
                        .getAddress();
                final String image = TextUtils.isEmpty(_station.getStationImg()) ? " " :
                        (BaseConfig.getFileUrl() + _station.getStationImg());
                String[] message=new String[]{HxMessage.Type.STATION+"",code,name,status,address,image};
                final Intent intentShare = new Intent(this, ShareActivity.class);
                intentShare.putExtra("message", message);
                startActivity(intentShare);
                break;
            case R.id.more:
                ImageActivity.showStationImg(this, _station.getStationCode());
                break;
            case R.id.video_img://华迈视频(互联网观看视频)
                initHuaMai();
                break;
            default:
                break;
        }

    }

    *//**
     * @desc 华迈视频相关初始化
     * @author songxf
     * created at 2017/7/10 16:00
     *//*
    HMDefines.LoginServerInfo mInfo;

    private void initHuaMai() {
        Thread serverThread = new Thread() {
            @Override
            public void run() {
                mInfo = new HMDefines.LoginServerInfo();
                mInfo.ip = "www.huamaiyun.com";//平台地址
                mInfo.port = 80; //平台端口
                mInfo.user = "hxsj";//用户名
                mInfo.password = "123456";//密码
                mInfo.model = android.os.Build.MODEL; // 手机型号
                mInfo.version = android.os.Build.VERSION.RELEASE; // 手机系统版本号
                String error = jni_connectServer();
                if (error != null) {
                    sendEmptyMessage(EVENT_CONNECT_FAIL, error);
                } else {
                    sendEmptyMessage(EVENT_CONNECT_SUCCESS);
                }
                super.run();
            }

        };
        serverThread.start();
    }

    public String jni_connectServer() {
        StringBuilder error = new StringBuilder();
        Log.d(TAG, "jni_connectServer: ---->" + mInfo);
        BaseApp.serverId = BaseApp.getJni().connectServer(mInfo, error);
        if (BaseApp.serverId == -1) {
            return error.toString();
        }
        return null;
    }


    private void sendEmptyMessage(int msgId, String error) {
        if (mHandler == null) {
            return;
        }
        Message msg = new Message();
        msg.what = msgId;
        msg.obj = error;
        mHandler.sendMessage(msg);
    }

    private void sendEmptyMessage(int msgId) {
        sendEmptyMessage(msgId, null);
    }

    private void registerHander() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case EVENT_LOGIN_SUCCESS:
                        // ToastUtil.showToast("登录成功");

                        Intent intent = new Intent(StationInfoActivity.this, HxHuaMaiVideoActivity.class);
                        intent.putExtra("stationCode", _stationCode);
                        startActivity(intent);
                        break;
                    case EVENT_LOGIN_FAIL:
                        BaseApp.getJni().disconnectServer(BaseApp.serverId);
                        //  ToastUtil.showToast("登录失败");
                        break;
                    case EVENT_CONNECT_SUCCESS:
                        int result = BaseApp.getJni().getDeviceList(BaseApp.serverId);
                        if (result != HMDefines.HMEC_OK) {
                            sendEmptyMessage(EVENT_LOGIN_FAIL);
                            return;
                        }
                        // step 2: Get user information.
                        HMDefines.UserInfo userInfo = BaseApp.getJni().getUserInfo(BaseApp.serverId);
                        if (userInfo == null) {
                            sendEmptyMessage(EVENT_LOGIN_FAIL);
                            return;
                        }
                        *//**
                         * TODO:判断选择： huamaiyun和see1000中需要添加userInfo.useTransferService !=8
                         *
                         * 这个判断 seebao中需要去掉，否则容易报错！
                         *//*
                        // step 3: Get transfer service.
                        // if (userInfo.useTransferService != 0&&userInfo.useTransferService !=8) {
                        if (userInfo.useTransferService != 0) {
                            result = BaseApp.getJni().getTransferInfo(BaseApp.serverId);
                            if (result != HMDefines.HMEC_OK) {
                                sendEmptyMessage(EVENT_LOGIN_FAIL);
                                return;
                            }
                        }

                        // step 4: Get tree id.
                        BaseApp.treeId = BaseApp.getJni().getTree(BaseApp.serverId);
                        sendEmptyMessage(EVENT_LOGIN_SUCCESS);
                        break;
                    case EVENT_CONNECT_FAIL:
                        //提示登录失败原因
                        ToastUtil.showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }


    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        ApiGetStationInfo(_stationCode, refreshView);
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        refreshView.onRefreshComplete();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    protected class VehicleRecordListAdapter extends RecyclerView.Adapter<VehicleRecordListAdapter.MyViewHolder> {

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MyViewHolder(mInflater.inflate(R.layout.item_device, parent, false));
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            holder.update(position);
        }


        @Override
        public int getItemCount() {
            return dataList == null ? 0 : dataList.size();
        }

        protected class MyViewHolder extends RecyclerView.ViewHolder {

            @BindView(R.id.d_img)
            ImageView      dImg;
            @BindView(R.id.svg)
            ImageButton    svg;
            @BindView(R.id.realData)
            ImageButton    realData;
            @BindView(R.id.d_name)
            TextView       dName;
            @BindView(R.id.d_sn)
            TextView       dSn;
            @BindView(R.id.d_state)
            TextView       dState;
            @BindView(R.id.d_read)
            TextView       dRead;
            @BindView(R.id.d_send)
            TextView       dSend;
            @BindView(R.id.item_view)
            RelativeLayout itemView;

            int position;

            public MyViewHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }

            public void update(int position) {
                this.position = position;
                Device data = dataList.get(position);
                loadNetImg(dImg, data.getImgUrl());
                loadText(dName, data.getDeviceName(), "佚名");
                loadText("SN号：", dSn, data.getSnCode(), "未配置");
                loadText("采集周期：", dRead, data.getReadCycle(), "未配置");
                loadText("传输周期：", dSend, data.getSendCycle(), "未配置");

                switch (data.getState()) {
                    case 0:
                        dState.setText("正常");
                        break;
                    case 1:
                        dState.setText("超时");
                        break;
                    case 2:
                        dState.setText("故障");
                        break;
                    case 3:
                        dState.setText("未初始化");
                        break;
                    default:
                        break;
                }
            }

            @OnClick({R.id.realData, R.id.svg, R.id.item_view})
            public void onClick(View v) {
                Intent intent;
                Device data = dataList.get(position);
                switch (v.getId()) {
                    case R.id.realData: //指标
                        intent = new Intent(StationInfoActivity.this, StationRealDataActivity
                                .class);
                        intent.putExtra(Constants.IK_DEVICE_CODE, data.getDeviceCode());
                        intent.putExtra("STATION_CODE", _stationCode);
                        startActivity(intent);
                        break;
                    case R.id.svg: //工艺图
                        intent = new Intent(StationInfoActivity.this, SVGShowActivity.class);
                        intent.putExtra(Constants.IK_DEVICE_CODE, data.getDeviceCode());
                        intent.putExtra(Constants.IK_STATION_CODE, _station.getStationCode());
                        startActivity(intent);
                        break;
                    case R.id.item_view:
                        BigDeviceInfoActivity.start(StationInfoActivity.this, data
                                .getDeviceCode());
                    default:
                        break;
                }
            }

        }
    }*/
}
